home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Networking / Network Watch (DMZ) v1.5 / sources / dMZLists.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-25  |  14.6 KB  |  633 lines  |  [TEXT/MPS ]

  1. /*
  2. #-------------------------------------------------------------------------------------------
  3. #
  4. #    Program:    < DMZ 1.3 >
  5. #    File:        < dmzLists.c >
  6. #    
  7. #    by pvh
  8. #    of <Apple Macintosh Developer Technical Support - or wheverever>
  9. #
  10. #    Modification History
  11. #    5/27/94  rrk     Made PPC compatible
  12. #    9/21/92  rrk    Got rid of "^2 ^3" string left in dialog at init time
  13. #                    if DMZ started on system with no zones.  Put in paramText
  14. #                    call in tellUserNoZones()
  15. #
  16. #    Copyright © 1990 Apple Computer, Inc.
  17. #    All rights reserved.
  18. #    
  19. #-------------------------------------------------------------------------------------------
  20. */
  21.  
  22. #include    "dmz.h"
  23.  
  24. /* 
  25.  *    External globals we use in this dude.
  26.  */
  27. extern     DialogPtr         gMyDialog;
  28. extern     long            gSortMode;
  29. extern  EventRecord        gMyEvent;
  30. extern  short            gATalkTransFlag;
  31. extern  myATQEntry        gATQEntry;
  32. /* 
  33.  *    Globals we use here
  34.  */
  35. ListHandle                 gZonesList, gObjectTypeList;
  36. char                     gNameGlob[34];
  37.  
  38.  
  39. /* 
  40.  *    This is a simple routine that invalidates the passed in dialog item's rectangle 
  41.  *    so that we get a cleaner refresh of only the items that need updating.
  42.  */
  43. void invalidateItem(short whichItem)
  44. {
  45.     Rect         r;
  46.     short         kind;
  47.     Handle         h;
  48.     GrafPtr     savedPort;
  49.     
  50.     GetPort(&savedPort);
  51.     SetPort(gMyDialog);
  52.     GetDialogItem(gMyDialog, whichItem, &kind, &h, &r);
  53.     InvalRect(&r);
  54.     BeginUpdate(gMyDialog);
  55.     UpdateDialog(gMyDialog, gMyDialog->visRgn);
  56.     EndUpdate(gMyDialog);
  57.     SetPort(savedPort);
  58. }
  59.     
  60.     
  61. void DisposOfMyLists()
  62. {
  63.     LDispose(gZonesList);
  64.     LDispose(gObjectTypeList);
  65. }
  66.     
  67. void OpenZonesList()
  68. {
  69.     Rect         dBounds, rView;
  70.     Point         cSize;
  71.     GrafPtr     tp;
  72.     short        kind;
  73.     Handle        h;
  74.     
  75.     GetPort(&tp);
  76.     SetPort(gMyDialog);
  77.     GetDialogItem(gMyDialog, 1, &kind, &h, &rView);
  78.  
  79.     rView.top += 1;
  80.     rView.bottom -= 1;
  81.     rView.right -= 16;
  82.  
  83.     cSize.v = 0;
  84.     cSize.h = 0;
  85.  
  86.     SetRect(&dBounds, 0, 0, 1, 0); 
  87.     gZonesList = LNew(&rView, &dBounds, cSize, 0, gMyDialog, true, false, false, true);
  88.     (**gZonesList).selFlags = lUseSense+lOnlyOne;
  89.     SetPort(tp);
  90. }
  91.  
  92. void tellUserNoZones()
  93. {
  94.     Cell             theCell;
  95.     short         ignore, i;
  96.     GrafPtr     tp;
  97.     
  98.     ClearZoneCells();        // quick way to clear out an old entry - sort of overkill
  99.     GetPort(&tp);
  100.     SetPort(gMyDialog);
  101.  
  102.     LSetDrawingMode(false, gZonesList);
  103.  
  104.     i = 0;
  105.  
  106.     theCell.v = i;
  107.     theCell.h = 0;
  108.  
  109.     ignore = LAddRow(1, i, gZonesList);    
  110.  
  111.     LClrCell(theCell, gZonesList);
  112.     if (((dmzEntryPtr) *(gATQEntry.globs))->atalkActive)  // If ATalk active.
  113.     {
  114.         LSetCell("No zones <*>.", PStrLen("\pNo zones <*>."), theCell, gZonesList);    
  115.         ParamText("\p", "\p", "\p", "\p");
  116.     }
  117.     else
  118.     {
  119.         LSetCell("AppleTalk is off.", PStrLen("\pAppleTalk is off."), theCell, gZonesList);    
  120.         ParamText("\p", "\p", "\p", "\p");
  121.     }
  122.  
  123.     LSetDrawingMode(true, gZonesList);
  124.     InvalRect(&(**gZonesList).rView);
  125.     SetPort(tp);
  126. }
  127.  
  128. /*****************************************************************************/
  129.  
  130.  
  131.  
  132. /* Compare two pascal-strings. */
  133.  
  134. #pragma segment StringUtils
  135. short    pcmp(StringPtr s1, StringPtr s2)
  136. {
  137.     short    i, len;
  138.     char    j;
  139.  
  140.     if ((len = s1[0]) > s2[0]) len = s2[0];
  141.  
  142.     for (i = 1; i <= len; ++i)
  143.     {
  144.         j = s1[i] - s2[i];
  145.         if (j != 0) 
  146.             return(j);
  147.     }
  148.  
  149.     return(s1[0] - s2[0]);
  150. }
  151.     
  152.  
  153. /* 
  154.  *    compare routine for qsort().  it uses a global selector for the different 'fields' inthe list
  155.  *    they are actually offsets to the data in each cell.
  156.  *    OK...so it's really gross.
  157.  */
  158. int myCompare(const void *aStr, const void *bStr)
  159. {
  160.     long    sortPtr;
  161.     int        result;
  162.     
  163.     switch(gSortMode) {
  164.         case sortOnZoneName:
  165.              sortPtr = myNetworkEntityObject;
  166.              break;
  167.         case sortOnType:
  168.              sortPtr = myNetworkEntityType;
  169.              break;
  170.         case sortOnNet:
  171.              sortPtr = myNetworkEntityNet;
  172.              break;
  173.         case sortOnNode:
  174.              sortPtr = myNetworkEntityNode;
  175.              break;
  176.         case sortOnSocket:
  177.              sortPtr = myNetworkEntitySocket;
  178.              break;
  179.         }
  180.     result = IUCompString((ConstStr255Param)((long)aStr + sortPtr), (ConstStr255Param)((long)bStr + sortPtr));
  181.     return result;
  182. //    return(IUCompString((void *)(aStr + sortPtr), (void *)(bStr + sortPtr)));
  183. }
  184.  
  185. /* 
  186.   *    call qsort() 
  187.   */
  188. void letsSort(Ptr theBuffPtr, short numZonesGot, short number)
  189. {        
  190.     SetPort(gMyDialog);
  191.     
  192.     /* tell them we're sorting */
  193.     ParamText("\p", "\p", "\p", "\psorting...");        
  194.     invalidateItem(kProgressID);
  195.         
  196.     /* do the sort */
  197. #ifdef __MWERKS__
  198.     qsort(theBuffPtr, (size_t)numZonesGot, (size_t)number, (_Cmpfun *)myCompare);
  199. #else
  200.     qsort(theBuffPtr, (size_t)numZonesGot, (size_t)number, myCompare);
  201. #endif
  202.     
  203.     /* erase the info text */
  204.     ParamText("\p", "\p", "\p", "\p");        
  205.     invalidateItem(kProgressID);
  206. }
  207.  
  208.  
  209. /* 
  210.  *    set up the zone list 
  211.  */
  212. void SetZoneCells(Ptr bufferPtr, short NumZonesGot)
  213. {
  214.     Cell         theCell;
  215.     short         ignore, i;
  216.     long         bufferIndex;
  217.     GrafPtr     tp;
  218.     Str32        myZone, zoneString;
  219.         
  220.     myZone[0] = 0;
  221.     getMyZone((char *)myZone);
  222.     
  223.     GetPort(&tp);
  224.     SetPort(gMyDialog);
  225.  
  226.     /* added 5/5/89 pvh */
  227.     gSortMode = sortOnZoneName;
  228.     letsSort(bufferPtr, NumZonesGot, zoneNameSize);
  229.     
  230.     LSetDrawingMode(false, gZonesList);
  231.     
  232.     bufferIndex = 0L;
  233.     i = 0;
  234.     while(i<NumZonesGot) {
  235.         ignore = LAddRow(1, i, gZonesList);    
  236.         theCell.v = i;
  237.         theCell.h = 0;
  238.         
  239.         SpinTheCursor();        
  240.         zoneString[0] = (char)(bufferPtr+bufferIndex)[0];
  241.         LSetCell((Ptr)bufferPtr+bufferIndex + 1L, zoneString[0], theCell, gZonesList);    
  242.  
  243.         /*
  244.          *    when we find our zone, select it in the list
  245.          */
  246.         zoneString[0] = (char)(bufferPtr+bufferIndex)[0];
  247.         BlockMove((Ptr)bufferPtr+bufferIndex + 1L, (Ptr)&zoneString + 1L, zoneString[0]);
  248.         if(EqualString(zoneString, myZone, false, false)) {
  249.             LSetSelect(true, theCell, gZonesList);
  250.             LAutoScroll(gZonesList);
  251.             getTypesNamesInZone((char *)zoneString);
  252.             }
  253.             
  254.         i += 1;
  255.         bufferIndex += zoneNameSize;
  256.         }
  257.  
  258.     LSetDrawingMode(true, gZonesList);
  259.  
  260.     SetPort(tp);
  261.  
  262. }
  263.  
  264. /* 
  265.  *    clear the zone list 
  266.  */
  267. void ClearZoneCells()
  268. {
  269.     GrafPtr     tp;
  270.     Str32        tempStr;
  271.  
  272.     GetPort(&tp);
  273.     SetPort(gMyDialog);
  274.     
  275.     ParamText("\p", "\p", "\p", "\p");    // clear the number of items text which appears in item 7
  276.  
  277.             // clear out the "number of found items" dialog item
  278.     tempStr[0] = 0;        
  279.     setItemString(gMyDialog, (short) kObjectCountID, tempStr);
  280.  
  281.     LSetDrawingMode(false, gZonesList);
  282.     
  283.     LDelRow(0,0, gZonesList);
  284.     EraseRect(&(**gZonesList).rView);
  285.  
  286.     LSetDrawingMode(true, gZonesList);
  287.     InvalRect(&(**gZonesList).rView);
  288.  
  289.     SetPort(tp);
  290.  
  291. }
  292.  
  293. /* 
  294.  *    handle click in the zone list box 
  295.  */
  296. void doZonesListStuff()
  297. {
  298.     Boolean     tempBool;
  299.     Cell         thisCell;
  300.     Rect         scrollRect;
  301.     GrafPtr     tp;
  302.     Str255          dataPtr;
  303.     short         dataLen;
  304.     Point        localPoint = gMyEvent.where;
  305.  
  306.     ListHandle    testH;
  307.     
  308.     thisCell.v = 0;
  309.     thisCell.h = 0;
  310.     GetPort(&tp);
  311.     SetPort(gMyDialog);
  312.     
  313.     if(gMyDialog != FrontWindow())
  314.         SelectWindow(gMyDialog);
  315.     else 
  316.     {
  317.         if (!((dmzEntryPtr) *(gATQEntry.globs))->atalkActive)  // ATalk not active.
  318.             return;
  319.         GlobalToLocal(&localPoint);
  320.         scrollRect = (**gZonesList).rView;
  321.         scrollRect.left = (**gZonesList).rView.right;
  322.         scrollRect.right = scrollRect.left + 15;
  323.         if(PtInRect(localPoint, &scrollRect))
  324.             tempBool = LClick(localPoint, gMyEvent.modifiers, gZonesList);
  325.         else if(PtInRect(localPoint, &(**gZonesList).rView)) {
  326.             if(LClick(localPoint, gMyEvent.modifiers, gZonesList)) {
  327.                 testH = gObjectTypeList;    
  328.                 LGetSelect(true, &thisCell, gZonesList);
  329.                 dataLen = 255;
  330.                 LGetCell(&dataPtr[1], &dataLen, thisCell, gZonesList);
  331.                 dataPtr[0] = dataLen;
  332.                 BlockMove(&dataPtr, &gNameGlob[0], sizeof(gNameGlob));
  333.                 getTypesNamesInZone((char *)dataPtr);
  334.                 }
  335.             else {
  336.                 LGetSelect(true, &thisCell, gZonesList);
  337.                 dataLen = 255;
  338.                 LGetCell(&dataPtr[1], &dataLen, thisCell, gZonesList);
  339.  
  340.                 dataPtr[0] = dataLen;
  341.  
  342.                 BlockMove(&dataPtr[0], &gNameGlob[0], sizeof(gNameGlob));
  343.                 }
  344.             }
  345.         }
  346.     SetPort(tp);
  347. }
  348.  
  349. void OpenObjectTypeList()
  350. {
  351.     Rect         dBounds, rView;
  352.     Point         cSize;
  353.     GrafPtr     tp;
  354.     Handle        h;
  355.     short        kind, ignore;
  356.     
  357.     GetPort(&tp);
  358.     SetPort(gMyDialog);
  359.     GetDialogItem(gMyDialog, 2, &kind, &h, &rView);
  360.  
  361.     rView.top += 1;
  362.     /*
  363.     // rView.bottom -= 1;
  364.     // rView.bottom = gMyDialog->portRect.bottom-16;
  365.     */
  366.     
  367.     rView.right -= 16;
  368.  
  369.     cSize.v = 0;
  370.     cSize.h = 0;
  371.  
  372.     SetRect(&dBounds, 0, 0, 1, 0); /* two cells across by 0 down extra cell for hiding entity net address */
  373.     gObjectTypeList = LNew(&rView, &dBounds, cSize, 128, gMyDialog, true, false, false, true);
  374.     (**gObjectTypeList).selFlags = lUseSense+lOnlyOne;
  375.     
  376.     SizeControl((**gObjectTypeList).vScroll, (**(**gObjectTypeList).vScroll).contrlRect.right - 
  377.         (**(**gObjectTypeList).vScroll).contrlRect.left, (**(**gObjectTypeList).vScroll).contrlRect.bottom - 
  378.         (**(**gObjectTypeList).vScroll).contrlRect.top - 16);
  379.     
  380.     ignore = LAddColumn(1, 0, gObjectTypeList);    
  381.  
  382.     SetPort(tp);
  383. }
  384.  
  385. void doObjectDoubleClick()
  386. {
  387.     Cell                 thisCell;
  388.     short                dataLen;
  389.     long                temp;
  390.     myNetworkEntity        myEnt;
  391.     AddrBlock            address;
  392.  
  393.     thisCell.v = 0;
  394.     thisCell.h = 0;
  395.     LGetSelect(true, &thisCell, gObjectTypeList);
  396.     
  397.     /* 
  398.      *    at this point do whatever you want with the object selection.
  399.      *    send 'em lots of packets and try and kill them or whatever...
  400.      */
  401.     dataLen = sizeof(myNetworkEntity);
  402.     LGetCell((Ptr) &myEnt, &dataLen, thisCell, gObjectTypeList);
  403.  
  404.     StringToNum((void *) myEnt.net, (long *) &temp);
  405.     address.aNet = (short) temp;
  406.     StringToNum((void *) myEnt.node, (long *) &temp);
  407.     address.aNode = (short) temp;
  408.     StringToNum((void *) myEnt.socket, (long *) &temp);
  409.     address.aSocket = (short) temp;
  410.     if (OpenTransportActive())
  411.         doOTEcho(&myEnt);
  412.     else
  413.         doEcho(&myEnt);
  414. }
  415.  
  416.  
  417. void doObjectTypeListStuff()
  418. {
  419.     Boolean             ignore;
  420.     Cell                 thisCell;
  421.     Rect                 scrollRect;
  422.     GrafPtr             tp;
  423.     Point                localPoint = gMyEvent.where;
  424.     
  425.     thisCell.v = 0;
  426.     thisCell.h = 0;
  427.     GetPort(&tp);
  428.     SetPort(gMyDialog);
  429.     
  430.     if(gMyDialog != FrontWindow())
  431.         SelectWindow(gMyDialog);
  432.     else {
  433.         if (!((dmzEntryPtr) *(gATQEntry.globs))->atalkActive)  // ATalk not active.
  434.             return;
  435.         GlobalToLocal(&localPoint);
  436.         scrollRect = (**gObjectTypeList).rView;
  437.         scrollRect.left = (**gObjectTypeList).rView.right;
  438.         scrollRect.right = scrollRect.left + 15;
  439.         if(PtInRect(localPoint, &scrollRect))
  440.             ignore = LClick(localPoint, gMyEvent.modifiers, gObjectTypeList);
  441.         else if(PtInRect(localPoint, &(**gObjectTypeList).rView)) {
  442.             /*
  443.              *    Check for a double click.
  444.              */
  445.              if(LClick(localPoint, gMyEvent.modifiers, gObjectTypeList)) {
  446.                 doObjectDoubleClick();
  447.                 }
  448.             }
  449.         }
  450.     SetPort(tp);
  451. }
  452.  
  453. /* 
  454.  *    clear the object and types list 
  455.  */
  456. void ClearObjectTypesList()
  457. {
  458.     GrafPtr     tp;
  459.     
  460.     GetPort(&tp);
  461.     SetPort(gMyDialog);
  462.  
  463.     LSetDrawingMode(false, gObjectTypeList);
  464.     
  465.     LDelRow(0,0, gObjectTypeList);
  466.     EraseRect(&(**gObjectTypeList).rView);
  467.  
  468.     LSetDrawingMode(true, gObjectTypeList);
  469.     InvalRect(&(**gObjectTypeList).rView);
  470.  
  471.     SetPort(tp);
  472.  
  473. }
  474.  
  475. /* 
  476.  *    this routine pads strings out to various lengths.  justification to right or left can 
  477.  *    be specified.  we need this for a cleaner display as well as insuring a correct sort
  478.  *    on numeric data (i.e. the strings '1234' and ' 1234' are not equivalant, but if
  479.  *    we justify to the right, we'll always be OK.  (whoops maybe not in the case of the Japanese
  480.  *    alphabet...damn...
  481.  */
  482. void padEntry(unsigned char *entry, short length, short just)
  483. {
  484.     unsigned char     index, i;
  485.     
  486.     switch(just) {
  487.         case leftJust:
  488.             index = entry[0];    /* get length byte */
  489.             index += 1;            /* increment by 1 */
  490.         
  491.             while(index <= length) {
  492.                 entry[index] = ' ';
  493.                 index += 1;
  494.                 }    
  495.             break;
  496.         case rightJust:
  497.             index = entry[0];    /* get length byte */
  498.         
  499.             if(index<length) {
  500.                 BlockMove(&entry[1], (Ptr)&entry[1]+length-index, (long)index);
  501.                 
  502.                 i = 1;
  503.                 while(index < length) {
  504.                     entry[i] = ' ';
  505.                     i += 1;
  506.                     index += 1;
  507.                     }    
  508.                 }
  509.             break;
  510.         }
  511.     entry[0] = length;
  512. }
  513.     
  514. /* 
  515.  *    this is called when the NBPLookup has finished.  the buffer and the number of obects found
  516.  *    is added to the list.
  517.  */
  518. void SetObjectTypeCells(Ptr bufferPtr, short numDevicesGot)
  519. {
  520.     Cell                 theCell;
  521.     short                 ignore;
  522.     short                 numDevicesIndex;
  523.     short                maxCells;
  524.     GrafPtr             tp;
  525.     AddrBlock             address;
  526.     EntityName            abEntity;
  527.     unsigned char        charHolder[6];
  528.     long                g;
  529.     myNetworkEntity     myNetEnt;
  530.     Ptr                    newBuffer;
  531.     char                tempStr[10];
  532.     
  533.     /* tell the user this may take a little while... */
  534.     /*waitAWhile = GetCursor(watchCursor);
  535.     SetCursor(*waitAWhile);*/
  536.     
  537.     GetPort(&tp);
  538.     SetPort(gMyDialog);
  539.  
  540.     /* make room for as many objects as we need */
  541.     newBuffer = NewPtr(numDevicesGot*sizeof(myNetworkEntity));
  542.     if(newBuffer == 0L)
  543.         return;
  544.         
  545.     LDelRow(0, 0, gObjectTypeList);    /* deletes lists's cells */
  546.     
  547.     LSetDrawingMode(false, gObjectTypeList);  /* turn drawing off */
  548.     
  549.     numDevicesIndex = 0;
  550.  
  551.         // since the List Manager will only allow 32K entries, we need to limit the number
  552.         // of entries that can be entered - numDevicesGot may be just too large, so we take
  553.         // only those first n entries in the list which will fit into the list
  554.     maxCells = numDevicesGot < (32767 / (sizeof(myNetworkEntity) + 4)) ? numDevicesGot : (32767 / (sizeof(myNetworkEntity) + 4)); 
  555.     while(numDevicesIndex < maxCells) 
  556.     {
  557.         theCell.v = numDevicesIndex;
  558.         
  559.         ignore = LAddRow(1, numDevicesIndex, gObjectTypeList);    
  560.  
  561.         NBPExtract(bufferPtr, numDevicesGot, numDevicesIndex+1, &abEntity, &address);
  562.  
  563.         /* first move address data into "hidden" cell */
  564.         theCell.h = 1;
  565.         LSetCell((Ptr) &address, 4, theCell, gObjectTypeList);
  566.         
  567.         /* now move object name & type into one cell */
  568.         theCell.h = 0;
  569.         
  570.         /* object */
  571.         BlockMove(&abEntity.objStr, &myNetEnt.object, 33L);    
  572.         if(myNetEnt.object[0] > 32)
  573.             myNetEnt.object[0] = 32;
  574.             
  575.         /* type */
  576.         BlockMove(&abEntity.typeStr, &myNetEnt.type, 33L);
  577.         if(myNetEnt.type[0] > 32)
  578.             myNetEnt.type[0] = 32;
  579.         
  580.         /* network */
  581.         g = (long)address.aNet;
  582.         g = g & 0x0000FFFF; /* mask out hiword crap */
  583.         NumToString(g, (void *) &charHolder);
  584.         padEntry((void *) &charHolder, 5, rightJust);
  585.         BlockMove(&charHolder, &myNetEnt.net, 6L);
  586.  
  587.         /* node */
  588.         NumToString((long)address.aNode, (void *) &charHolder);
  589.         padEntry((void *) &charHolder, 3, rightJust);
  590.         BlockMove(&charHolder, &myNetEnt.node, 4L);
  591.     
  592.         /* socket */
  593.         NumToString((long)address.aSocket, (void *) &charHolder);
  594.         padEntry((void *) &charHolder, 3, rightJust);
  595.         BlockMove(&charHolder, &myNetEnt.socket, 4L);
  596.                 
  597.         BlockMove((Ptr) &myNetEnt, (Ptr)newBuffer+(numDevicesIndex)*sizeof(myNetworkEntity), 
  598.             sizeof(myNetworkEntity));
  599.  
  600.         LSetCell((Ptr)&myNetEnt, sizeof(myNetworkEntity), theCell, gObjectTypeList);    
  601.  
  602.         numDevicesIndex += 1; 
  603.         }
  604.  
  605.     /* do a quicksort() on the mess */
  606.     letsSort(newBuffer, numDevicesGot, sizeof(myNetworkEntity));
  607.     
  608.     numDevicesIndex = 0;
  609.     
  610.     while(numDevicesIndex<maxCells) {
  611.         theCell.v = numDevicesIndex;
  612.         theCell.h = 0;
  613.         BlockMove((Ptr)newBuffer+(numDevicesIndex)*sizeof(myNetworkEntity), (Ptr) &myNetEnt, sizeof(myNetworkEntity));
  614.         LSetCell((Ptr)&myNetEnt, sizeof(myNetworkEntity), theCell, gObjectTypeList);    
  615.         numDevicesIndex += 1;
  616.         }
  617.         
  618.     DisposePtr(newBuffer);
  619.     
  620.     NumToString(numDevicesIndex, (void *) tempStr);
  621.  
  622.     ParamText((ConstStr255Param)tempStr, "\p# of objects: ", "\p", "\p");
  623.  
  624.     LSetDrawingMode(true, gObjectTypeList);
  625.     invalidateItem(8);
  626.     invalidateItem(2);
  627.  
  628.     SetPort(tp);
  629.     InitCursor();
  630. }
  631.  
  632.  
  633.